Java syntax
part 16/23 Β· 86.1 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
, the compiler can safely assume that methods and variables computable at
compile-time
may be inlined.
Varargs
This language feature was introduced in J2SE 5.0. The last argument of the method may be declared as a variable arity parameter, in which case the method becomes a variable arity method (as opposed to fixed arity methods) or simply varargs method. This allows one to pass a variable number of values, of the declared type, to the method as parameters - including no parameters. These values will be available inside the method as an array.
void printReport(String header, int... numbers) { //numbers represents varargs
System.out.println(header);
for (int num : numbers) {
System.out.println(num);
}
}
// Calling varargs method
printReport("Report data", 74, 83, 25, 96);
Fields
Fields, or class variables, can be declared inside the class body to store data.
class Foo {
double bar;
}
Fields can be initialized directly when declared.
class Foo {
double bar = 2.3;
}
Modifiers
β’ static - Makes the field a static member.
β’ final - Allows the field to be initialized only once in a constructor or inside initialization block or during its declaration, whichever is earlier.
β’ transient - Indicates that this field will not be stored during serialization.
β’ volatile - If a field is declared volatile, it is ensured that all threads see a consistent value for the variable.
Inheritance
Classes in Java can only inherit from one class. A class can be derived from any class that is not marked as final. Inheritance is declared using the extends keyword. A class can reference itself using the this keyword and its direct superclass using the super keyword.
class Foo {
}
class Foobar extends Foo {
}
If a class does not specify its superclass, it implicitly inherits from java.lang.Object class. Thus all classes in Java are subclasses of Object class.
If the superclass does not have a constructor without parameters the subclass must specify in its constructors what constructor of the superclass to use. For example:
class Foo {
public Foo(int n) {
// Do something with n
}
}
class Foobar extends Foo {
private int number;
// Superclass does not have constructor without parameters
// so we have to specify what constructor of our superclass to use and how
public Foobar(int number) {
super(number);
this.number = number;
}
}
Overriding methods
Unlike C++, all non-final methods in Java are virtual and can be overridden by the inheriting classes.
class Operation {
public int doSomething() {
return 0;
}
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ